<template>
  <view class="update-modal" v-if="visible">
    <view class="modal-mask"></view>
    <view class="modal-content">
      <view class="modal-body">
        <view class="update-header">
          <text class="title">发现新版本</text>
          <text class="version">V{{ versionInfo.version }}</text>
        </view>

        <view class="update-content">
          <text class="content-title">更新内容：</text>
          <text class="content-text">{{ versionInfo.releaseNotes }}</text>
        </view>

        <!-- 下载状态显示 -->
        <view class="download-status" v-if="downloadStatus !== 'not_started'">
          <progress
            :percent="downloadProgress"
            stroke-width="3"
            activeColor="#ff9500"
            backgroundColor="#f5f5f5"
            v-if="downloadStatus === 'downloading'"
          />
          <text class="status-text">
            {{
              downloadStatus === "downloading"
                ? `正在下载(${downloadProgress}%)`
                : "下载完成，可以安装了"
            }}
          </text>
        </view>

        <view class="update-tip" v-if="downloadStatus === 'not_started'">
          <text>发现新版本，建议立即更新体验最新功能</text>
        </view>
        <view class="update-tip" v-else-if="downloadStatus === 'completed'">
          <text>新版本已下载完成，立即安装体验最新功能</text>
        </view>

        <view class="modal-footer">
          <button class="btn-cancel" @click="handleCancel">
            {{ cancelText }}
          </button>
          <button
            class="btn-update"
            @click="handleAction"
            :disabled="downloadStatus === 'downloading'"
          >
            {{ actionText }}
          </button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import updateUtils from "@/utils/updateUtils";

export default {
  name: "UpdateModal",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    versionInfo: {
      type: Object,
      default: () => ({}),
    },
    downloadStatus: {
      type: String,
      default: "not_started", // not_started, downloading, completed
    },
    downloadProgress: {
      type: Number,
      default: 0,
    },
  },
  computed: {
    actionText() {
      switch (this.downloadStatus) {
        case "not_started":
          return "立即下载";
        case "downloading":
          return "下载中...";
        case "completed":
          return "立即安装";
        default:
          return "立即下载";
      }
    },
    cancelText() {
      return this.downloadStatus === "completed" ? "稍后安装" : "稍后再说";
    },
  },
  methods: {
    handleCancel() {
      this.$emit("cancel");
    },
    handleAction() {
      if (this.downloadStatus === "not_started") {
        this.$emit("download");
      } else if (this.downloadStatus === "completed") {
        this.$emit("install");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.update-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;

  .modal-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
  }

  .modal-content {
    position: relative;
    width: 600rpx;
    background: #fff;
    border-radius: 24rpx;
    overflow: hidden;
    animation: modalShow 0.3s ease-out;
  }

  .modal-body {
    padding: 40rpx;
  }

  .update-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 30rpx;
    position: relative;

    .rocket-icon {
      width: 160rpx;
      height: 160rpx;
      margin-bottom: 20rpx;
    }

    .title {
      font-size: 36rpx;
      font-weight: 500;
      color: #333;
      margin-bottom: 12rpx;
    }

    .version {
      font-size: 28rpx;
      color: #666;
    }
  }

  .update-content {
    background: #f8f9fa;
    border-radius: 16rpx;
    padding: 24rpx;
    margin-bottom: 30rpx;
    max-height: 300rpx;
    overflow-y: auto;

    .content-title {
      font-size: 28rpx;
      color: #333;
      margin-bottom: 16rpx;
      display: block;
    }

    .content-text {
      font-size: 26rpx;
      color: #666;
      line-height: 1.6;
    }
  }

  .download-status {
    margin-bottom: 30rpx;

    progress {
      width: 100%;
      margin-bottom: 10rpx;
    }

    .status-text {
      display: block;
      text-align: center;
      font-size: 26rpx;
      color: #666;
    }
  }

  .update-tip {
    text-align: center;
    font-size: 26rpx;
    color: #ff9500;
    margin-bottom: 30rpx;
    padding: 0 20rpx;
    line-height: 1.5;
  }

  .modal-footer {
    display: flex;
    justify-content: space-between;
    gap: 20rpx;

    button {
      flex: 1;
      height: 80rpx;
      line-height: 80rpx;
      text-align: center;
      border-radius: 40rpx;
      font-size: 28rpx;
      font-weight: 500;
      border: none;

      &::after {
        border: none;
      }

      &:disabled {
        opacity: 0.6;
        color: #999;
      }
    }

    .btn-cancel {
      background: #f5f5f5;
      color: #666;
    }

    .btn-update {
      background: #ff9500;
      color: #fff;

      &:disabled {
        background: #ffc069;
      }
    }
  }
}

@keyframes modalShow {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}
</style>
